home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8604 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  87 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!milod
  3. From: milod@netcom.com (John DiCamillo)
  4. Subject: Re: Interpreting tokens in C++???
  5. Message-ID: <milodDMu3qJ.B96@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <312238A1.4F70@cadman.cit.buffalo.edu>
  8. Date: Thu, 15 Feb 1996 20:32:43 GMT
  9. Sender: milod@netcom16.netcom.com
  10.  
  11. nvp@acsu.buffalo.edu writes:
  12.  
  13. >Hello, I am trying to write C++ code that considers a theoretical
  14. >language (TLAN), and through two classes (and re-casting), interprets
  15. >tokens from the TLAN to have meaning in C++.
  16.  
  17. >BTW, these programs are not being compiled (no error checking for now),
  18. >but are interpreted by the code.
  19.  
  20. OK, so you're writing a simple interpreter.
  21. Why just two classes?  Is that a constraint of the
  22. homework problem?  (BTW, is this a compiler class
  23. or a computer architecture class?)
  24.  
  25. >Here's a sample TLAN program:
  26.  
  27. >BEGIN // ignored by interpreter.
  28. >SET I 10 // i = 10
  29. >SET V 1  // v = 1
  30. >WHILE V
  31. >    BEGIN
  32. >    INC V // v++
  33. >        DEC I // i--
  34. >        PRINT V // cout << v
  35. >NEWLINE // endl
  36. >END
  37. >END // program end
  38.  
  39. >I am presently using #define to define each token (using an integer)
  40. >and strtok (from the c-library token.h).
  41.  
  42. Here, I suspect you have your terminology confused.
  43. I think you are using #define to define token *types*.
  44. In other words, "#define SET 10" defines a token
  45. type SET.  The words in the program text that are
  46. equal to "SET" are the *tokens*, and they are instances
  47. of the token type SET.
  48.  
  49. >For a statement like SET I 10, how would I get token I to = token 10?
  50.  
  51. >Strtok breaks them up into tokens, but I am having problems with
  52. >interpreting the meaning of statements after that ...
  53.  
  54. Well, you have a couple ways to go, depending on whether
  55. you will be using this program as the basis for future
  56. work.  The right way to do things is to define a lexicon
  57. and grammar for TLAN and build a parser and scanner using
  58. lex and yacc (or equivalent -- I prefer to use object-
  59. oriented scanning and parsing classes, as described in
  60. http://www.qds.com/people/jdicamillo/oosp.htm).  Then you
  61. use these tools to construct an abstract syntax tree from
  62. the program text and interpret that.
  63.  
  64. The quicker way to do this (given that TLAN is tiny and you
  65. probably are going to learn the other stuff later anyways)
  66. is to define a class "Variable" or "Symbol" that you can use
  67. to represent program variables.  This class has a string
  68. name, and an integer value.  Then when your interpreter
  69. sees the statement "SET I 10", it creates a new Variable by
  70. supplying the name "I" and the value 10.  Hint: you probably
  71. want to store all of your Variable in an environment or
  72. symbol table for easy look-up later.
  73.  
  74. Then your only problem is dealing with WHILE-BEGIN-END.
  75. Oh well, at least you don't have scoping and closures to deal
  76. with...yet. :-)
  77.  
  78. >Nathan V. Patwardhan
  79. >nvp@cs.buffalo.edu
  80.  
  81. -- 
  82.     ciao,
  83.     milo
  84. ================================================================
  85.     John DiCamillo                         Fiery the Angels Fell 
  86.     milod@netcom.com       Deep thunder rode around their shores
  87.